package com.example.safeassistan.home.fragment;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseVPFragment;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.home.adapter.RiskDetailPersonFragmentAdapter;
import com.example.safeassistan.home.adapter.RiskDetailRiskFragmentAdapter;
import com.example.safeassistan.home.adapter.RiskDetailTableFragmentAdapter;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.risk.activity.RiskPointTableDetailActivity;
import com.example.safeassistan.risk.model.RiskPointPersonModel;
import com.example.safeassistan.risk.model.RiskPointtableModel;
import com.example.safeassistan.utile.JsonUtils;
import com.example.safeassistan.widget.ViewPagerForScrollView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RiskDetailTableFragment extends BaseVPFragment {
    private Context context;
    private View view;
    private RecyclerView rcView;
    private LinearLayout llNodata;

    private RiskDetailTableFragmentAdapter adapter;
    private ViewPagerForScrollView pager;
    private String riskpointId="";

    public RiskDetailTableFragment(ViewPagerForScrollView pager,String riskpointId) {
        this.pager = pager;
        this.riskpointId=riskpointId;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.riskdetail_table_fragment, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    public void initView(){
        pager.setObjectForPosition(view,5);
        rcView=view.findViewById(R.id.rcView);
        llNodata=view.findViewById(R.id.llNodata);
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {

    }

    @Override
    protected void onFragmentFirstVisible() {
        getData();
    }

    public void getData(){
        Map map=new HashMap();
        map.put("id",riskpointId);
        new HttpRequest(context).doGet(UrlConstants.getRiskPointTable, null, map,new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), RiskPointtableModel.class);
                        if (obj instanceof RiskPointtableModel) {
                            RiskPointtableModel model=(RiskPointtableModel) obj;
                            if (model.getResult().size()>0){
                                llNodata.setVisibility(View.GONE);
                                adapter=new RiskDetailTableFragmentAdapter(model.getResult());
                                LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false){
                                    @Override
                                    public boolean canScrollVertically() {
                                        return false;
                                    }
                                };
                                rcView.setLayoutManager(linearLayoutManager);
                                rcView.setAdapter(adapter);
                                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                        if (model.getResult().get(position).getDangerVOList()!=null&&model.getResult().get(position).getDangerVOList().size()>0){
                                        RiskPointTableDetailActivity.actionStart(context,model.getResult().get(position).getId(),
                                                model.getResult().get(position).getTableName(),
                                                model.getResult().get(position).getDangerVOList(),riskpointId);
                                        }else {
                                            Toast.makeText(context,"该检查表没有绑定危险源",Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }else {
                                llNodata.setVisibility(View.VISIBLE);
                            }
                        }
                    }else {
                        Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }
}